import 'package:flutter/material.dart';
import 'package:flutter_widget/src/config/color_define.dart';
import 'package:flutter_widget/src/widget/effect/glass_widget.dart';
import 'package:flutter_widget/src/widget/image/web_cache_Image.dart';
import 'package:flutter_widget/src/widget/tap_widget.dart';

///模糊层（可设置模糊背景图）
class VisualEffectImageWidget extends StatelessWidget {
  final String? coverUrl;
  final String? coverUrlPlaceholder;
  final String? localImage;
  final Function? onTap;
  final double? opacity;
  final Color? coverColor;

  const VisualEffectImageWidget({
    Key? key,
    this.coverUrl,
    this.coverUrlPlaceholder,
    this.onTap,
    this.opacity,
    this.coverColor,
    this.localImage,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TapWidget(
      onTap: () {
        if (onTap != null) {
          onTap!();
        }
      },
      child: coverUrlWidget(),
    );
  }

  Widget coverUrlWidget() {
    return Stack(
      //重叠的Stack Widget，实现重贴
      children: <Widget>[
        ConstrainedBox(
          //约束盒子组件，添加额外的限制条件到 child上。
          constraints: const BoxConstraints.expand(), //限制条件，可扩展的。
          child: localImage?.isNotEmpty == true
              ? Image.asset(
                  localImage!,
                  fit: BoxFit.cover,
                )
              : coverUrl?.isNotEmpty == true
                  ? WebCacheImage(
                      fit: BoxFit.cover,
                      errorImage: coverUrlPlaceholder,
                      placeholderImage: coverUrlPlaceholder,
                      imageUrl: coverUrl ?? "",
                      retry: false,
                      scale: WebCacheImageScale.middle,
                    )
                  : Image.asset(
                    coverUrlPlaceholder!,
                    fit: BoxFit.cover,
                  ),
        ),
        Center(
          child: ClipRect(
            child: GlassWidget(
              child: Opacity(
                opacity: opacity ?? 0.2,
                child: Container(
                  decoration: BoxDecoration(
                    color: coverColor ?? FWColorPicker.colorFF333333,
                  ),
                ),
              ),
            ),
          ),
        )
      ],
    );
  }
}
